import Mock from 'mockjs'
import qs from 'qs'

// 基本配置
Mock.setup({
  // 模拟网络延迟
  timeout: '500-1000',
})

// 拦截接口
// 1. 请求地址路径规则
// 2. 请求方式
// 3. 返回数据的格式
// Mock.mock(/\/my\/test/, 'get', { id: '@id', name: '@ctitle(2,4)' })

// 模拟 我的收藏
Mock.mock(/\/member\/collect/, 'get', (config) => {
  // 截取 ？后面的查询字符串
  const queryString = config.url.split('?')[1]
  // 对查询字符串解析成对象的形式，然后解构取出来
  const { pageSize, page } = qs.parse(queryString)
  // 数据集合
  const items = []
  // 根据传递的条数，生成多少条数据
  for (let i = 0; i < +pageSize; i++) {
    items.push(
      Mock.mock({
        id: '@id',
        name: '@ctitle(10,20)',
        desc: '@ctitle(4,10)',
        price: '@float(100,200,2,2)',
        picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock(
          '@integer(1,8)',
        )}.jpg`,
      }),
    )
  }
  return {
    msg: '获取收藏商品成功',
    result: {
      counts: 35,
      pageSize: +pageSize,
      page: +page,
      items,
    },
  }
})

// 我的足迹
Mock.mock(/\/member\/browseHistory/, 'get', (config) => {
  const queryString = config.url.split('?')[1]
  const { pageSize, page } = qs.parse(queryString)
  let items = []
  for (let i = 0; i < +pageSize; i++) {
    items.push(
      Mock.mock({
        id: '@id',
        name: '@ctitle(10,20)',
        desc: '@ctitle(4,10)',
        price: '@float(100,200,2,2)',
        picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock(
          '@integer(1,8)',
        )}.jpg`,
      }),
    )
  }

  console.log('items', items)

  return {
    msg: '操作成功',
    result: {
      counts: 35,
      pageSize: +pageSize,
      page: +page,
      items,
    },
  }
})
